package com.ctshk.app.order.air.controller;

import com.alibaba.fastjson.JSON;
import com.ctshk.common.annotations.OperationLogAnnotation;
import com.ctshk.common.dto.Result;
import com.ctshk.common.enums.ChannelMainData;
import com.ctshk.common.enums.OperationLogModel;
import com.ctshk.common.enums.OperationLogType;
import com.ctshk.common.req.pay.NotifyConsumeReq;
import com.ctshk.common.web.support.BaseController;
import com.ctshk.rpc.order.air.dto.app.OrderDetailDTO;
import com.ctshk.rpc.order.air.req.AirOrderCancelReq;
import com.ctshk.rpc.order.air.req.app.AddOrderReq;
import com.ctshk.rpc.order.air.req.app.AppPayOrderReq;
import com.ctshk.rpc.order.air.service.app.IAirProductOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @BelongsProject: ctshk-cloud
 * @BelongsPackage: com.ctshk.app.order.air.controller
 * @Author: hef
 * @CreateTime: 2021-05-20 14:01
 * @Description: 描述
 */
@Api(value = "PC端机票订单服务",tags = "PC端机票订单服务")
@Slf4j
@RestController
@RequestMapping("/pc/air-product-order")
public class PcAirProductOrderController extends BaseController {

    @DubboReference
    private IAirProductOrderService orderService;

    @PostMapping("/addOrder")
    @ApiOperation(value = "新增PC机票订单", notes = "新增PC机票订单")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.ADD, operTitle = "新增PC机票订单", operDesc = "新增PC机票订单")
    public Result<Long> addOrder(@RequestBody@Valid AddOrderReq req){
        req.setChannelId(ChannelMainData.CHINA_TRAVEL.getId());
        return Result.success(orderService.addOrder(req,getCurrentUserAllowNotLogIn(getRequest())));
    }

    @PostMapping("/payOrder")
    @ApiOperation(value = "支付PC机票订单", notes = "支付PC机票订单")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.ADD, operTitle = "支付PC机票订单", operDesc = "支付PC机票订单")
    public Result payOrder(@RequestBody AppPayOrderReq req){
        req.setCallBackUrl("/app/order/air/pc/air-product-order/payCallback");
        return Result.success(orderService.payOrder(req,getCurrentUserAllowNotLogIn(getRequest())));
    }

    @PostMapping("/payCallback")
    @ApiOperation(value = "支付订单回调", notes = "支付订单回调",hidden = true)
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.UPDATE, operTitle = "支付订单回调", operDesc = "支付订单回调")
    public Result payCallback(@RequestBody NotifyConsumeReq req){
        log.info("收到支付回调,参数*************:{}", JSON.toJSONString(req));
        return Result.success(orderService.payCallback(req));
    }


    @PutMapping("/cancelOrder")
    @ApiOperation(value = "取消订单", notes = "取消订单")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.UPDATE, operTitle = "取消订单", operDesc = "取消订单")
    public Result<Boolean> cancelOrder(@RequestBody @Valid AirOrderCancelReq req){
        return Result.success(orderService.cancelOrder(req,getCurrentUserAllowNotLogIn(getRequest())));
    }

    @GetMapping("/getOrderDetail/{orderId}")
    @ApiOperation(value = "查询订单详情", notes = "查询订单详情")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.QUERY, operTitle = "查询订单详情", operDesc = "查询订单详情")
    public Result<OrderDetailDTO> getOrderDetail(@PathVariable Long orderId){
        return Result.success(orderService.getOrderDetail(orderId));
    }

}
